<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE root PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:a4j="http://richfaces.org/a4j"
	xmlns:rich="http://richfaces.org/rich">

	<h:outputText rendered="#{clienteBean.saveState}">
		<div class="innerTitle">Cadastro de Cliente</div>
	</h:outputText>

	<h:outputText rendered="#{clienteBean.updateState}">
		<div class="innerTitle">Atualizacao de Cliente</div>
	</h:outputText>

	<h:inputHidden value="#{clienteBean.entity.id}" rendered="#{clienteBean.updateState}"/>

	<rich:tabPanel switchType="client">

        <rich:tab label="Cadastro">
			<div>
            <h:panelGrid columns="3" styleClass="crud" columnClasses="label">

				<h:outputLabel value="Nome :"/>
				<h:inputText id="nomeCliente" value="#{clienteBean.entity.nome}" required="true" maxlength="50" size="50">
					<f:validateLength maximum="50" minimum="3"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="nomeCliente"/>

				<h:outputLabel value="Razão Social :"/>
				<h:inputText id="razaoSocialCliente" value="#{clienteBean.entity.razaoSocial}" required="true" maxlength="150" size="80">
					<f:validateLength maximum="150" minimum="3"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="razaoSocialCliente"/>

				<h:outputLabel value="Tipo :"/>
				<h:selectOneMenu id="tipoDocumentoCliente" value="#{clienteBean.entity.tipoDocumento}" required="true">
	  				<f:selectItems value="#{clienteBean.tipoDocumentos}" />
				</h:selectOneMenu>
				<rich:message errorClass="error" for="tipoDocumentoCliente"/>

				<h:outputLabel value="Num. Documento :"/>
				<h:inputText id="documentoCliente" value="#{clienteBean.entity.numeroDocumento}" required="true" maxlength="18" size="18">
					<f:validateLength maximum="18" minimum="11"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="documentoCliente"/>

				<h:outputLabel value="Tel. Comercial :"/>
				<h:inputText id="telefoneComercialCliente" value="#{clienteBean.entity.telefoneComercial}" required="true" maxlength="20" size="20">
					<f:validateLength maximum="20" minimum="10"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="telefoneComercialCliente"/>

				<h:outputLabel value="Fax. :"/>
				<h:inputText id="faxCliente" value="#{clienteBean.entity.telefoneFax}" maxlength="20" size="20">
					<f:validateLength maximum="20" minimum="10"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="faxCliente"/>

				<h:outputLabel value="Celular :"/>
				<h:inputText id="celularCliente" value="#{clienteBean.entity.telefoneCelular}" maxlength="20" size="20">
					<f:validateLength maximum="20" minimum="10"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="celularCliente"/>

			</h:panelGrid>
			</div>
        </rich:tab>

        <rich:tab label="Endereco">

        	<h:panelGrid columns="3" styleClass="crud" columnClasses="label">

        		<h:outputLabel value="Descrição :"/>
				<h:inputText id="descricaoEnderecoCliente" value="#{clienteBean.entity.descricao}" required="true" maxlength="150" size="70">
					<f:validateLength maximum="150" minimum="2"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="descricaoEnderecoCliente"/>

				<h:outputLabel value="Número :"/>
				<h:inputText id="numeroEnderecoCliente" value="#{clienteBean.entity.numero}" required="true" maxlength="4" size="6">
					<f:validateLength maximum="4" minimum="1"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="numeroEnderecoCliente"/>

				<h:outputLabel value="Complemento :"/>
				<h:inputText id="complementoEnderecoCliente" value="#{clienteBean.entity.complemento}" required="false" maxlength="30" size="30">
					<f:validateLength maximum="30" minimum="2"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="complementoEnderecoCliente"/>

				<h:outputLabel value="Estado :"/>
				<h:selectOneMenu id="estadoCliente" value="#{clienteBean.entity.bairro.estado.id}" required="true">
	  				<f:selectItems value="#{estadoBean.listaEstados}"/>
					<a4j:support event="onchange" action="#{clienteBean.populate}" reRender="cidadeCliente"/>
				</h:selectOneMenu>
				<rich:message errorClass="error" for="estadoCliente"/>

				<h:outputLabel value="Cidade :"/>
				<h:selectOneMenu id="cidadeCliente" value="#{clienteBean.entity.bairro.cidade.id}"  required="true">
	  				<f:selectItems value="#{clienteBean.cidades}"/>
	  				<a4j:support event="onchange" action="#{clienteBean.populate}" reRender="bairroCliente"/>
				</h:selectOneMenu>
				<rich:message errorClass="error" for="cidadeCliente"/>

				<h:outputLabel value="Bairro :"/>
				<h:selectOneMenu id="bairroCliente" value="#{clienteBean.entity.bairro.id}"  required="true">
	  				<f:selectItems value="#{clienteBean.bairros}"/>
				</h:selectOneMenu>
				<rich:message errorClass="error" for="bairroCliente"/>

				<h:outputLabel value="MSN :"/>
				<h:inputText id="msnCliente" value="#{clienteBean.entity.enderecoMSN}" maxlength="50" size="40">
					<f:validateLength maximum="50" minimum="10"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="msnCliente"/>

				<h:outputLabel value="Skype :"/>
				<h:inputText id="skypeCliente" value="#{clienteBean.entity.enderecoSkype}" maxlength="50" size="40">
					<f:validateLength maximum="50" minimum="10"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="skypeCliente"/>

				<h:outputLabel value="E-mail :"/>
				<h:inputText id="emailCliente" value="#{clienteBean.entity.enderecoEmail}" maxlength="50" size="40">
					<f:validateLength maximum="50" minimum="10"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="emailCliente"/>

			</h:panelGrid>

        </rich:tab>

        <rich:tab label="Contrato" rendered="#{controlBean.administradorSistema}">

			<h:panelGrid columns="3" styleClass="crud" columnClasses="label">

				<h:outputLabel value="Numero Contrato :"/>
				<h:inputText id="numeroContratoCliente" value="#{clienteBean.entity.numeroContrato}" required="true" maxlength="12" size="12">
					<f:validateLength maximum="10" minimum="1"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="numeroContratoCliente"/>

				<h:outputLabel value="Descricao Contrato :"/>
				<h:inputText id="descricaoContratoCliente" value="#{clienteBean.entity.descricaoContrato}" required="true" maxlength="50" size="50">
					<f:validateLength maximum="50" minimum="8"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="descricaoContratoCliente"/>

				<h:outputLabel value="Num.Veiculos :"/>
				<h:inputText id="numeroVeiculosCliente" value="#{clienteBean.entity.numeroVeiculos}" required="true" maxlength="10" size="10">
					<f:validateLongRange minimum="1"  maximum="200"/>
					<rich:ajaxValidator event="onblur"/>
				</h:inputText>
				<rich:message errorClass="error" for="numeroVeiculosCliente"/>

				<h:outputLabel value="Data Contrato :"/>
				<rich:calendar id="dataContratoCliente" value="#{clienteBean.entity.dataContrato}" required="true" datePattern="dd/MM/yyyy"/>
				<rich:message errorClass="error" for="dataContratoCliente"/>

				<h:outputLabel value="Data Validade :"/>
				<rich:calendar id="dataValidadeContrato" value="#{clienteBean.entity.dataValidadeContrato}" required="true" datePattern="dd/MM/yyyy"/>
				<rich:message errorClass="error" for="dataValidadeContrato"/>

			</h:panelGrid>

        </rich:tab>

		<rich:tab label="Permissões" rendered="#{controlBean.administradorSistema}">

			<h:panelGroup id="permissoesCliente" styleClass="innerFilter" layout="block">

				<h:selectOneMenu value="#{clienteBean.permissao.id}" rendered="#{clienteBean.permissaoStatus}">
	  				<f:selectItems value="#{clienteBean.listaPermissoes}"/>
				</h:selectOneMenu>

				<a4j:commandButton value="Adicionar" action="#{clienteBean.addPermissao}" styleClass="button" reRender="permissoesCliente" disabled="#{!clienteBean.permissaoStatus}"/>

				<rich:dataTable rowClasses="even,odd" value="#{clienteBean.entity.permissoes}" var="permissao" rows="10">

					<h:column>
						<f:facet name="header"><h:outputLabel value="Permissões"/></f:facet>
						<h:outputText value="#{permissao.descricao}"/>
					</h:column>

					<h:column headerClass="link">
						<a4j:commandLink action="#{clienteBean.delPermissao}" reRender="popup">
							<h:graphicImage styleClass="link" alt="Excluir" url="/images/btn/delete.png"/>
							<f:setPropertyActionListener target="#{clienteBean.permissao}" value="#{permissao}"/>
						</a4j:commandLink>
					</h:column>

		            <f:facet name="footer">
						<rich:datascroller renderIfSinglePage="false" reRender="permissoesCliente"/>
		            </f:facet>

				</rich:dataTable>

			</h:panelGroup>

		</rich:tab>

    </rich:tabPanel>

	<br/>

	<a4j:commandButton value="Inserir" type="submit" action="#{clienteBean.save}" styleClass="button" rendered="#{clienteBean.saveState}" reRender="popup"/>
	<a4j:commandButton value="Atualizar" type="submit" action="#{clienteBean.update}" styleClass="button" rendered="#{clienteBean.updateState and controlBean.administradorSistema}" reRender="popup"/>
	<a4j:commandButton value="Atualizar" type="submit" action="#{clienteBean.atualizarCliente}" styleClass="button" rendered="#{clienteBean.updateState and controlBean.usuarioAdministrador and (not controlBean.administradorSistema)}"  reRender="popup"/>
	<a4j:commandButton value="Voltar" type="submit" action="#{clienteBean.search}" styleClass="button" immediate="true" reRender="popup"/>

</ui:composition>